<template>
    <el-dialog :title="$t('common.editBtn')"  v-model="visible" width="700"
        :close-on-click-modal="false" draggable>
        <el-form :model="dataForm" :rules="dataRules" label-width="110px" ref="dataFormRef" v-loading="loading">
            <el-row>
                <el-col :span="12" class="mb-6">
                    <el-form-item :label="$t('bar.receiptCode')" prop="sourceCode">
                        <el-input style="width: 85%" :placeholder="$t('bar.receiptCode')" v-model="dataForm.sourceCode" :disabled="true"/>
                    </el-form-item>
                </el-col>
                <el-col :span="12" class="mb-6">
                    <el-form-item :label="$t('bar.barcode')" prop="barcode">
                        <el-input style="width: 85%" :placeholder="$t('bar.barcode')" v-model="dataForm.barcode" :disabled="true"/>
                    </el-form-item>
                </el-col>
                <el-col :span="12" class="mb-6">
                    <el-form-item :label="$t('bar.materialcode')" prop="materialcode">
                        <el-input style="width: 85%" :placeholder="$t('bar.materialcode')" v-model="dataForm.materialCode" :disabled="true"/>
                    </el-form-item>
                </el-col>
                <el-col :span="12" class="mb-6">
                    <el-form-item :label="$t('bar.currentqty')" prop="currentQty">
                        <el-input style="width: 85%" clearable :placeholder="$t('bar.currentqty')" v-model="dataForm.currentQty" />
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <template #footer>
        <span class="dialog-footer">
          <el-button @click="visible = false" icon="Close">{{$t('inProduction.cancel')}}</el-button>
          <el-button type="primary" @click="onSubmit" :disabled="loading" icon="Check">{{$t('inProduction.confirm')}}</el-button>
        </span>
    </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { nextTick, onMounted, reactive, ref } from 'vue';
import {updReceiptBarcodeQty} from '/@/api/platform/wms/reprintbarcode';
import {useMessage} from "/@/hooks/message";
import {useI18n} from 'vue-i18n';
const emits = defineEmits(['getChildren'])

const {t} = useI18n();
// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);
const loading = ref(false);


const dataForm = reactive({
    sourceCode: '',
    barcode: '',
    materialCode: '',
    currentQty: '',
})

// 定义表单校验规则
const dataRules = reactive({
    currentQty:[
    // 限制数字
    {pattern:/^\d*\.?\d*$/, message: t('inProduction.enterNumbersOnly'), trigger: 'blur'},
    {required: true, message: t('bar.qtyError'), trigger: 'blur'},
  ],
})

// 提交
const onSubmit = async () => {
  const valid = await dataFormRef.value.validate().catch(() => {
  });
  if (!valid) return false;

  try {
    loading.value = true;
    await updReceiptBarcodeQty(dataForm);
    useMessage().success( t('inProduction.editSuccess'));
    visible.value = false;
    emits('getChildren', loading);
  } catch (err: any) {
    useMessage().error(err.msg);
  } finally {
    loading.value = false;
  }
};


// 编辑弹框
const openEditDialog = (data: any) => {
    visible.value = true;
    // 将data中的数据解构到dataForm中
    Object.assign(dataForm, data);
}

// 暴露变量
defineExpose({
     openEditDialog
});
</script>
